package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.ExpressCompany;
import com.dingreading.cloud.admin.service.ExpressCompanyService;
import com.dingreading.cloud.admin.util.bean.ExpressUtil;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.google.common.collect.Lists;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/system/expressCompany")
@Api(tags = "总部-快递公司管理")
public class ExpressCompanyController {

    @Resource
    ExpressCompanyService expressCompanyService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "商品名") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        Page<ExpressCompany> page = this.expressCompanyService.pageList(pageUtil, name, enabled);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }


    @ApiOperation(value = "添加/编辑商品种类")
    @PostMapping(value = "/editBean", produces = "application/json; charset=UTF-8")
    public R<Object> editBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "商品id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "商品名") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "状态 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        if (id == null || id <= 0) {
            // 新增
            ExpressCompany category = ExpressUtil.newExpress(new ExpressCompany(), name, enabled);
            expressCompanyService.saveOrUpdate(category);
        } else {
            // 修改
            ExpressCompany data = expressCompanyService.getById(id);
            if (data == null) return R.fail("获取失败");

            ExpressUtil.newExpress(data, name, enabled);
            expressCompanyService.saveOrUpdate(data);
        }
        return R.ok();
    }


    @ApiOperation(value = "获取可用的快递公司")
    @PostMapping(value = "/getList", produces = "application/json; charset=UTF-8")
    public R<Object> getList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "快递公司名") @RequestParam(value = "name", required = false) String name
    ) {
        List<ExpressCompany> list = this.expressCompanyService.getByEnabled(name);
        return R.ok(list);
    }


    @ApiOperation(value = "修改启用状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "启用状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        this.expressCompanyService.updateEnabled(Lists.newArrayList(id), enabled);
        return R.ok();
    }


    @ApiOperation(value = "获取快递公司详情")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        ExpressCompany data = expressCompanyService.getById(id);
        if (data == null) return R.fail("获取失败");

        return R.ok(data);
    }


    @ApiOperation(value = "删除快递公司")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        ExpressCompany data = expressCompanyService.getById(id);
        if (data == null) return R.fail("获取失败");

        expressCompanyService.removeById(id);
        return R.ok();
    }

}